﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;

namespace ASP.NET5.Helper
{
    /// <summary>
    /// 定义Connectionstrings节点类
    /// </summary>
    public class Connectionstrings
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }
    public class AppsettingsHelper
    {

        //获取指定节点ConnectionStrings下的某A库的连接字符串。
        public static string A = "";
        //超时秒数
        public static readonly int CommandTimeout = Convert.ToInt32(Build()["DefaultConfig:CommandTimeout"].ToString());
        //AppsettingsPath
        public static string AppsettingsPath = "appsettings.json";

        //根据环境设置配置文件路径
        public static void GetConfigurationRoot(string environmentName = null)
        {
            AppsettingsPath = $"appsettings.{environmentName}.json"; //Startup 中初始环境变量
            //获取指定节点ConnectionStrings下的某A库的连接字符串。
            A = GetAppSettings<Connectionstrings>("ConnectionStrings", path: AppsettingsPath).A;
        }

        /// <summary>
        /// 获取当前环境
        /// </summary>
        /// <remarks>
        /// <para/>Author   :  xyg
        /// <para/>Date     :  2023-02-24
        /// </remarks>
        /// <returns>环境</returns>
        public static string CurrentEnvironment()
        {
            return Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            
        }

        /// <summary>
        /// 是否生产环境
        /// </summary>
        /// <remarks>
        /// <para/>Author   :  xyg
        /// <para/>Date     :  2023-02-24
        /// </remarks>
        /// <returns>是否生产环境</returns>
        public static bool IspProduction()
        {
            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            if (!string.IsNullOrEmpty(environment))
            {
                return environment.ToLower() == "production";
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <remarks>
        /// <para/>Author   :  xyg
        /// <para/>Date     :  2023-02-24
        /// </remarks>
        /// <returns>配置文件信息</returns>
        public static IConfiguration Build()
        {
            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            if (!string.IsNullOrEmpty(environment))
            {
                return Build(environment);
            }
            else
            {
                return Build("");
            }
            
        }

        /// <summary>
        /// 获取当前环境的配置文件
        /// </summary>
        /// <remarks>
        /// <para/>Author   :  xyg
        /// <para/>Date     :  2023-02-24
        /// </remarks>
        /// <returns>当前环境配置文件信息</returns>
        public static IConfiguration Build(string jsonFile)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{jsonFile}.json", true, reloadOnChange: true);
            var config = builder.Build();
            return config;
        }        

        public static T GetAppSettings<T>(string key, string path = "appsettings.json", bool reloadOnChange = true) where T : class, new()
        {
            IConfiguration config = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = path, ReloadOnChange = reloadOnChange })
            .Build();

            T appconfig = new ServiceCollection()
                .AddOptions()
                .Configure<T>(config.GetSection(key))
                .BuildServiceProvider()
                .GetService<IOptions<T>>()
                .Value;

            return appconfig;
        }

    }
}
